<?php
class StatisticsSeasonTeam extends AppModel {

	var $name = 'StatisticsSeasonTeam';
	var $primaryKey = 'ID';

	/* ------------------------------------------------------------------------ */
	/**
	 *
	 */
	function afterFind($results, $primary) {
		if( $primary == false ) {
			if( isset($results['iWins'])  &&
					isset($results['iDraws']) &&
				isset($results['iLoss'])) {
				$results['iMatch'] = $results['iWins'] + $results['iDraws'] + $results['iLoss'];

				if($results['iMatch'] != 0) {
					$results['fWins'] = 100 * $results['iWins'] / $results['iMatch'];
					$results['fDraws'] = 100 * $results['iDraws'] / $results['iMatch'];
					$results['fLoss'] = 100 * $results['iLoss'] / $results['iMatch'];
				} else {
					$results['fWins'] = 0;
					$results['fDraws'] = 0;
					$results['fLoss'] = 0;
				}
			}
		} else {
		foreach ($results as &$team) {
				foreach( $team as $key => $val ) {
					if( isset($team[$key]['iWins'])  &&
							isset($team[$key]['iDraws']) &&
								isset($team[$key]['iLoss'])) {
						$team[$key]['iMatch'] = $team[$key]['iWins'] + $team[$key]['iDraws'] + $team[$key]['iLoss'];

						if($team[$key]['iMatch'] != 0) {
							$team[$key]['fWins'] = 100 * $team[$key]['iWins'] / $team[$key]['iMatch'];
							$team[$key]['fDraws'] = 100 * $team[$key]['iDraws'] / $team[$key]['iMatch'];
							$team[$key]['fLoss'] = 100 * $team[$key]['iLoss'] / $team[$key]['iMatch'];
						} else {
							$team[$key]['fWins'] = 0;
							$team[$key]['fDraws'] = 0;
							$team[$key]['fLoss'] = 0;
						}
					}
				}
			}
		}
		return $results;
	}
}
?>